package cn.edu.scau.cmi.tangqingning.controll;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.scau.cmi.tangqingning.dao.StudentDao;
import cn.edu.scau.cmi.tangqingning.dao.UserDao;
import cn.edu.scau.cmi.tangqingning.dao.impl.StudentDaoImpl;
import cn.edu.scau.cmi.tangqingning.dao.impl.UserDaoImpl;
import cn.edu.scau.cmi.tangqingning.model.Group;
import cn.edu.scau.cmi.tangqingning.model.Student;
import cn.edu.scau.cmi.tangqingning.model.User;

/**
 * 控制层-逻辑处理以及页面跳转<br>
 * 当客户使用get方式请求Servlet时，Web容器调用doGet方法处理请求，判断method字符串来实现各种功能
 * @author 唐庆宁
 */
public class PreServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private String indentity;

	/**
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException 如果一个错误发生，则抛出此异常
	 * @throws IOException 如果一个错误发生，则抛出此异常
	 */
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * 重写doGet方法
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException 如果一个错误发生，则抛出此异常
	 * @throws IOException 如果一个错误发生，则抛出此异常
	 */

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if ("addStudent".equals(method)) {
			addStudent(request, response);
		} else if ("updateStudent".equals(method)) {
			updateStudent(request, response);
		} else if ("updateUserPassword".equals(method)) {
			indentity = method; // 更新管理员的密码
			preupdatetepwd(request, response);
		} else if ("updateStuPassword".equals(method)) {
			indentity = method; // 更新学生的密码
			preupdateStuPassword(request, response);
		} else if ("showPersonalInformation".equals(method)) {
			showPersonalInformation(request, response);
		} else if ("showProjectContent".equals(method)) {
			showProjectContent(request, response);
		} else if ("showTeamInformation".equals(method)) {
			showTeamInformation(request, response);
		} else if ("evaluateGroup".equals(method)) {
			evaluateGroup(request, response);
		} else if ("addGroup".equals(method)) {
			addGroup(request, response);
		} else if ("updateGroup".equals(method)) {
			updateGroup(request, response);
		} else if ("updateUserPassword".equals(indentity) && method == null) {
			updatetepwd(request, response);
		} else if ("updateStuPassword".equals(indentity) && method == null) {
			updateStuPassword(request, response);
		}
	}

	/**
	 * 评价小组信息-实现页面跳转
	 * 
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException
	 * @throws IOException
	 * @see cn.edu.scau.cmi.tangqingning.dao.impl.StudentDaoImpl#findProjectContentByGroupNumber
	 */
	private void evaluateGroup(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int number = Integer.parseInt(request.getParameter("groupNumber"));
		StudentDao studentDao = new StudentDaoImpl();
		Group group = studentDao.findProjectContentByGroupNumber(number);
		request.setAttribute("group", group);
		String studentMainPage = "evaluateGroup.jsp";
		request.setAttribute("studentMainPage", studentMainPage);
		RequestDispatcher dispatcher = request.getRequestDispatcher("studentMain.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * 添加学生项目小组-实现页面跳转
	 * 
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException
	 * @throws IOException
	 */
	private void addGroup(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String userMainPage = "addGroup.jsp";
		request.setAttribute("userMainPage", userMainPage);
		RequestDispatcher dispatcher = request.getRequestDispatcher("userMain.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * 查看学生个人信息-实现页面跳转
	 * 
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException
	 * @throws IOException
	 * @see cn.edu.scau.cmi.tangqingning.dao.impl.StudentDaoImpl#findStudentById
	 */
	private void showPersonalInformation(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		String stId = (String) session.getAttribute("username"); // 获取学生学号，以便显示个人信息
		StudentDaoImpl studentdao = new StudentDaoImpl();
		Group group = studentdao.findStudentById(stId);
		request.setAttribute("student", group.getStudent());
		request.setAttribute("group", group);
		String studentMainPage = "personalInformation.jsp";
		request.setAttribute("studentMainPage", studentMainPage);
		RequestDispatcher dispatcher = request.getRequestDispatcher("studentMain.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * 查看学生个人项目内容-实现页面跳转
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException
	 * @throws IOException
	 * @see cn.edu.scau.cmi.tangqingning.dao.impl.StudentDaoImpl#findProjectContentById
	 */
	private void showProjectContent(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		String stId = (String) session.getAttribute("username"); // 获取学生学号，以便显示小组信息
		StudentDaoImpl studentDao = new StudentDaoImpl();
		Group group = studentDao.findProjectContentById(stId);
		request.setAttribute("student", group.getStudent());
		request.setAttribute("group", group);
		String studentMainPage = "projectContent.jsp";
		request.setAttribute("studentMainPage", studentMainPage);
		RequestDispatcher dispatcher = request.getRequestDispatcher("studentMain.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * 学生互评-实现页面跳转
	 * 
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException
	 * @throws IOException
	 * @see cn.edu.scau.cmi.tangqingning.dao.impl.StudentDaoImpl#searchOtherGroup
	 */
	private void showTeamInformation(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		StudentDaoImpl groupList = new StudentDaoImpl();
		List list = groupList.searchOtherGroup();
		request.setAttribute("list", list);
		String studentMainPage = "teamInformation.jsp";
		request.setAttribute("studentMainPage", studentMainPage);
		RequestDispatcher dispatcher = request.getRequestDispatcher("studentMain.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * 添加学生的信息前-实现页面跳转
	 * 
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException 如果一个错误发生，则抛出此异常
	 * @throws IOException 如果一个错误发生，则抛出此异常
	 */
	public void addStudent(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String userMainPage = "addStudent.jsp";
		request.setAttribute("userMainPage", userMainPage);
		RequestDispatcher dispatcher = request.getRequestDispatcher("userMain.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * 更新学生的信息前-先查询出用户的信息，并实现页面跳转
	 * 
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException 如果一个错误发生，则抛出此异常
	 * @throws IOException 如果一个错误发生，则抛出此异常
	 * @see cn.edu.scau.cmi.tangqingning.dao.impl.UserDaoImpl#findStudentById
	 */
	public void updateStudent(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String stId = request.getParameter("stId");
		UserDao userDao = new UserDaoImpl();
		Student student = userDao.findStudentById(stId);
		request.setAttribute("student", student);
		String userMainPage = "updateStudent.jsp";
		request.setAttribute("userMainPage", userMainPage);
		RequestDispatcher dispatcher = request.getRequestDispatcher("userMain.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * 更新小组信息前-先查询出用户的信息，并实现页面跳转
	 * 
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException
	 * @throws IOException
	 * @see cn.edu.scau.cmi.tangqingning.dao.impl.UserDaoImpl#findGroupBygGroupNumber
	 */
	private void updateGroup(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String groupNumber = request.getParameter("groupNumber");
		UserDao userDao = new UserDaoImpl();
		Group group = userDao.findGroupBygGroupNumber(groupNumber);
		request.setAttribute("group", group);
		String userMainPage = "updateGroup.jsp";
		request.setAttribute("userMainPage", userMainPage);
		RequestDispatcher dispatcher = request.getRequestDispatcher("userMain.jsp");
		dispatcher.forward(request, response);

	}

	/**
	 * 更改管理员密码-实现页面跳转
	 * 
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException 如果一个错误发生，则抛出此异常
	 * @throws IOException 如果一个错误发生，则抛出此异常
	 */
	public void preupdatetepwd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String userMainPage = "updatepwd.jsp";
		request.setAttribute("userMainPage", userMainPage);
		RequestDispatcher dispatcher = request.getRequestDispatcher("userMain.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * 更改管理员密码-实现页面跳转
	 * 
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException 如果一个错误发生，则抛出此异常
	 * @throws IOException 如果一个错误发生，则抛出此异常
	 * @see cn.edu.scau.cmi.tangqingning.dao.impl.UserDaoImpl#updateUserPassWord
	 */
	public void updatetepwd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		String username = (String) session.getAttribute("username");
		String repassword = request.getParameter("repassword");
		User user = new User(username, repassword);
		UserDaoImpl userDao = new UserDaoImpl();
		int a = userDao.updateUserPassWord(user);
		if (a != 0) {
			String userMainPage = "updatepwdSuccess.jsp";
			request.setAttribute("userMainPage", userMainPage);
		} else {
			request.setAttribute("error", "更新失败");
		}
		RequestDispatcher dispatcher = request.getRequestDispatcher("userMain.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * 更改学生密码-实现页面跳转
	 * 
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException 如果一个错误发生，则抛出此异常
	 * @throws IOException 如果一个错误发生，则抛出此异常
	 */
	public void preupdateStuPassword(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String studentMainPage = "updatepwd.jsp";
		request.setAttribute("studentMainPage", studentMainPage);
		RequestDispatcher dispatcher = request.getRequestDispatcher("studentMain.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * 更改学生密码-实现页面跳转
	 * 
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException 如果一个错误发生，则抛出此异常
	 * @throws IOException 如果一个错误发生，则抛出此异常
	 * @see cn.edu.scau.cmi.tangqingning.dao.impl.StudentDaoImpl#updateStuPassWord
	 */
	public void updateStuPassword(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		String username = (String) session.getAttribute("username");
		String repassword = request.getParameter("repassword");
		Student student = new Student(Long.parseLong(username), Long.parseLong(repassword));
		StudentDaoImpl studentDao = new StudentDaoImpl();
		int a = studentDao.updateStuPassWord(student);
		if (a != 0) {
			String studentMainPage = "updatepwdSuccess.jsp";
			request.setAttribute("studentMainPage", studentMainPage);
		} else {
			request.setAttribute("error", "更新失败");
		}
		RequestDispatcher dispatcher = request.getRequestDispatcher("studentMain.jsp");
		dispatcher.forward(request, response);
	}

}
